var Global = require("../util/global.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {
        bg:{
            type:cc.Sprite,
            default:null,
        },
        title:{
            type:cc.Label,
            default:null,
        },
        bulletPanel:{
            type:cc.Layout,
            default:null,
        },
        toBulletPanel:{
            type:cc.Layout,
            default:null,
        },
        bulletItem:{
            type:cc.Prefab,
            default:null,
        },
        optionIndexs:null,

    },

    // onLoad () {},

    start () {
        this.init();
    },
    init(){
        //this.mock();
        this.initData();
    },
    mock(){
        Global.isWin = 1;
        Global.bullets = [
            {
                subject:"题目1",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"A",
                choose:"A",
            },
            {
                subject:"题目2",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"B",
                choose:"A",
            },
            {
                subject:"题目3",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"C",
                choose:"A",
            },
        ];
        Global.toBullets = [
            {
                subject:"题目1",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"A",
                choose:"B",
            },
            {
                subject:"题目2",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"B",
                choose:"A",
            },
            {
                subject:"题目3",
                optionList:[
                    "选项一",
                    "选项二",
                    "选项三",
                    "选项四"
                ],
                answer:"C",
                choose:"C",
            },
        ];
    },
    initData(){
        //背景旋转
        this.bg.node.runAction(cc.repeatForever(cc.rotateBy(100,360)));

        this.optionIndexs = ['A','B','C','D','E','F','G'];

        //战斗结果
        if(Global.isWin>0){
            this.title.string = "战斗胜利";
        }else if(Global.isWin==0){
            this.title.string = "双方平局";
        }else if(Global.isWin<0){
            this.title.string = "战斗失败";
        }

        //我方战斗详情
        for(var i=0;i<3;i++){
            var bullet = Global.toBullets[i];
            var bulletItem = cc.instantiate(this.bulletItem);
            bulletItem.getChildByName('id').getComponent(cc.Label).string="第"+(i+1)+"题";
            if(bullet){
                bulletItem.getChildByName('subject').getComponent(cc.Label).string=bullet.subject;
                if(bullet.choose)bulletItem.getChildByName('choose').getComponent(cc.Label).string=bullet.choose+"."+bullet.optionList[this.optionIndexs.indexOf(bullet.choose)];;
                bulletItem.getChildByName('answer').getComponent(cc.Label).string=bullet.answer+"."+bullet.optionList[this.optionIndexs.indexOf(bullet.answer)];
                if(bullet.choose!=bullet.answer)bulletItem.color=cc.color(255,0,0,255);
            }else{
                bulletItem.color=cc.color(255,0,0,255);
            }
            this.bulletPanel.node.addChild(bulletItem);
        }

        //敌方战斗详情
        for(var i=0;i<3;i++){
            var bullet = Global.bullets[i];
            var bulletItem = cc.instantiate(this.bulletItem);
            bulletItem.getChildByName('id').getComponent(cc.Label).string="第"+(i+1)+"题";
            if(bullet){
                bulletItem.getChildByName('subject').getComponent(cc.Label).string=bullet.subject;
                if(bullet.choose)bulletItem.getChildByName('choose').getComponent(cc.Label).string=bullet.choose+"."+bullet.optionList[this.optionIndexs.indexOf(bullet.choose)];
                bulletItem.getChildByName('answer').getComponent(cc.Label).string=bullet.answer+"."+bullet.optionList[this.optionIndexs.indexOf(bullet.answer)];
                if(bullet.choose!=bullet.answer)bulletItem.color=cc.color(255,0,0,255);
            }else{
                bulletItem.color=cc.color(255,0,0,255);
            }
            this.toBulletPanel.node.addChild(bulletItem);
        }
    },
    /**
     * 重新开始游戏
     * @return {[type]} [description]
     */
    onRestartGame(){
        message.type = 5;
        message.content = "战斗结束";
        Global.socket.sendData(message);
        cc.director.loadScene('start'); 
    }

    // update (dt) {},
});
